package com.anycl.design.structural.flyweight;

import java.util.HashMap;

/**
 * 享元工厂
 */
public class FlyweightFactory {

    private static HashMap<String, Flyweight> pool = new HashMap<>();

    // 获取池中对象
    public static Flyweight getFlyweight(String extrinsic) {
        Flyweight flyweight = null;
        if (!pool.containsKey(extrinsic)) {
            flyweight = new ConcreteFlyweight(extrinsic);
            pool.put(extrinsic, flyweight);
        } else {
            flyweight = pool.get(extrinsic);
        }
        return flyweight;
    }
}
